local Config = require("Config")
local Util = require("common.Util")
local Log = require("common.Log")
local Msg = require("core.Msg")
local Map = require("core.Map")
local Obj = require("core.Obj")
local ObjHuman = require("core.ObjHuman")
local ObjMonster = require("core.ObjMonster")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local Buffer = require("buffer.Buffer")
local RoleDefine = require("role.RoleDefine")
local CopyHandler = require("copy.HandlerL")
local ExcelMapConf = require("excel.Map")
local CommonDefine = require("common.CommonDefine")
local TaskLogic = require("task.TaskLogic")
local BeginStory = require("scene.BeginStory")
local TaskDefine = require("task.Define")
local MailLogicFL = require("mail.MailLogicFL")
local ConfigHandleL = require("config.HandlerL")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local SkillNotify   = require("skill.Notify")
local RoleRevive = require("role.RoleRevive")
local VipLogic = require("vip.VipLogic")
local CowryBagLogic = require("fuli.CowryBagLogic")
local MySteryShop = require("shop.MySteryShop")
local PetLogic = require("pet.PetLogic")
local ObjCollect = require("core.ObjCollect")
local RoleUIControl = require("role.RoleUIControl")
local TaskHelp  = require("task.TaskHelp")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local HorseLogic = require("horse.HorseLogic")
local JZWar = require("copy.JZWar")
local ItemExcel = require("excel.Item").item
local Status = require("scene.Status")

d3 = {}
local h_info
NO_GM = nil


local help_cehua = {
    additem = "添加一个道具 参数为id,cnt,bind,strengthLv,quality",
    additem2 = "添加一个道具 参数为道具名,cnt",
    mail = "发一测试邮件(邮件内容)",
    addexp = "添加人物经验",
    yinliang = "添加银两(数量)",
    yuanbao  = "添加元宝(数量)",
    bindyuanbao = "绑定元宝(数量)",
    buf = "添加一个buf",
    buffer = "查看BUFFER信息",
    clearbuf= "删除buf",
    pos = "位置",
    lv = "等级",
    copy ="copyid",
    skillcd = "清skill cd(下线失效)",
    clearbag = "清空背包",
    kill = "杀死某人，不支持怪物，参数为账号名",
    kick = "踢某人下线，参数为账号名",
    positionmove = "移动到当前地图的指定点",
    savedb = "立即保存数据库",
    gettask = "接取任务，后跟任务id",

    map = "跳转到地图mapid 不能是副本",
    mob = "召唤怪物 id 数量",
    zhangong = "战功值",
    script = "剧情副本ID",
    shenbing ="神兵ID 强化等级 星级",
    useskill = "技能ID 技能等级",
    fcm_online = "设置防沉迷在线时间",
    fcm = "1 通过，0 未通过",
    horse = "坐骑相关，坐骑等阶，坐骑星级",
    awake = "觉醒值",
    testday = "跨天",
    chongzhi = "充值  人民币",
    clearcowry = "清空寻宝仓库",
    sex = "改变角色性别，1为男,2为女",
    jihuoPet = "激活限时神将",
    collect = "召唤采集物,数量",
    finishnew = "跳过新手副本",
    clearmonster = "清本场景怪物",
    pata = "设置当前挑战关",
    addequip = "一次加一套装备，0-15",
    addsb = "神兵激活并设置等级",
    yellow = "黄钻等级1-8，是否是黄钻，1是,0否",
    qpoint = "购买彩票",
    name = "改名,新名字", 
    addtitle = "添加新称号，称号id",
    official = "测试军衔",
}

local buf = {}
for k, v in pairs(help_cehua) do
    buf[#buf + 1] = k .. "=" .. v
end
table.sort(buf)
h_info = table.concat(buf, "\n")


local function send(human, err)
    local mm = Msg.gc.GC_CHAT
    mm.uuid = human._id
    mm.obj_id = human.id
    mm.role_name = human.name
    mm.msg_type = Msg.cg[Msg.proto_id.CG_CHAT].msg_type
    mm.blue = ""
    mm.level3366 = 0
    mm.msg = tostring(err)
    mm.args = 0
    Msg.send(mm, human.fd)
end




function checkGm(human, str)
    if not Config.IS_USE_GM_CMD and human.db.gm == 0 then
        return
    end
    local pos = string.find(str, " ", 1, true)
    local cmd = string.sub(str, 1, pos and pos - 1)
    if not d3[cmd] then
        return
    end

    if not Config.IS_USE_GM_CMD then
        if human.db.gm == 1 and not GmControl[cmd] then -- 一级权限的gm只能使用部分gm命令
            send(human, "not permit for this gm cmd!")
            return true
        end
        if human.db.gm == 100 then -- 新手引导员不能使用gm命令
            return
        end
    end

    local arg = string.sub(str, #cmd + 2)
    Log.write(Log.LOGID_OSS_GM_CODE, human._id, human.account, human.name, cmd, arg)
    d3[cmd](human, tonumber(arg) or arg)
    send(human, "yes")
    return true
end

function d3.pos(human)
    send(human, h_info)
end
function d3.help(human)
    send(human, h_info)
end
function d3.clearCowry(human)
    for i=0, ItemDefine.COWRY_MAX_CAP do
        human.db.cowryBag[i].id = 0
    end
    CowryBagLogic.sendCowryBagList(human) 
end

function d3.additem(human, str)
    local id,cnt,bind,strengthLv,quality
    if type(str) ~= "number" then
        local pos = string.find(str, " ", 1, true)
        id = tonumber(string.sub(str, 1, pos and pos - 1))
        str = str.sub(str, pos + 1)
        pos = str.find(str, " ", 1, true)
        cnt = tonumber(str.sub(str, 1, pos and pos - 1) or str)
        if pos then
            str = str.sub(str, pos + 1)
            pos = str.find(str, " ", 1, true)
            bind = tonumber(str.sub(str, 1, pos and pos - 1) or str)
            if pos then
                str = str.sub(str, pos + 1)
                pos = str.find(str, " ", 1, true)
                strengthLv = tonumber(str.sub(str, 1, pos and pos - 1) or str)        
                quality = tonumber(str.sub(str, -1))
            end
        end
    else
        id = str
    end
    BagLogic.addItem(human, id, bind or 0, cnt or 1, "gm", quality, nil, nil,strengthLv)
end

function d3.additem2(human ,str)
    local name,cnt
    local pos = string.find(str, " ", 1, true)
    name = string.sub(str, 1, pos and pos - 1)
    str = str.sub(str, pos + 1)
    pos = str.find(str, " ", 1, true)
    cnt = tonumber(str.sub(str, 1, pos and pos - 1) or str)
    for k,v in pairs(ItemExcel) do
        if v.name == name then
            BagLogic.addItem(human, k, 0, cnt or 1, "gm")
            return
        end
    end
end

function d3.addequip(human,val)
    for i=1,10 do
        BagLogic.addItem(human, val * 10 + 20000 + i, 1, 1, "gm")
    end  
end
function d3.addsb(human, val)
    local sbConfig = require("excel.Shenbing").shenbing
    local ShenbingLogic = require("shenbing.ShenbingLogic")
    for i=1, #sbConfig do
        local tmpconfig = sbConfig[i]
        ObjHuman.addShenbing(human, i, tmpconfig.initStar)
        local data = ObjHuman.getShenbing(human, i)
        data.lv = val
        for j=1, 4 do
            data.skillLv[j-1] = 1
        end
        ShenbingLogic.updateShenbing(human, i)
    end  
end

function d3.awake(human, val)
    ObjHuman.addAwake(human, val)
end

function d3.name(human, name)
    local msg = Msg.lw.LW_GM
    msg.cmd = "changeName"
    msg.arg = name
    Msg.send2world(msg, human._id)
end

function d3.clearbag(human)
    for i=0, human.db.bagLen - 1 do
        human.db.bag[i].id = 0
        BagLogic.sendBagItemChange(human, i)
    end
    ObjHuman.doCalc(human)
end

function d3.horse(human, str)
    local pos = string.find(str, " ", 1, true)
    local stage = tonumber(string.sub(str, 1, pos and pos - 1))
    local star = tonumber(string.sub(str, pos + 1))

    human.db.horseStarLv = star
    human.db.horseStageLv = stage
    ObjHuman.doCalc(human)
end

function d3.yellow(human, str)
    local pos = string.find(str, " ", 1, true)
    local yellowLv = tonumber(string.sub(str, 1, pos and pos - 1))
    str = str.sub(str, pos + 1)
    pos = str.find(str, " ", 1, true)
    local yearYellow = tonumber(str.sub(str, 1, pos and pos - 1) or str)
    local blueSuperVip = tonumber(string.sub(str, pos + 1))
    --    str = str.sub(str, pos + 1)
    --    pos = str.find(str, " ", 1, true)
    --    bind = tonumber(str.sub(str, 1, pos and pos - 1) or str)

    --  local pos = string.find(str, " ", 1, true)
    --  local yellowLv = tonumber(string.sub(str, 1, pos and pos - 1))
    --  local yearYellow = tonumber(string.sub(str, pos + 1))
    human.db.blueYearVip = yearYellow
    human.db.blueVip = yellowLv == 0 and 0 or 1
    human.db.blueVipLv = yellowLv
    human.db.blueSuperVip = blueSuperVip
    local msg = Msg.lw.LW_GM
    msg.cmd = "blue"
    msg.arg = ""
    Msg.send2world(msg, human._id)
end

function d3.sex(human, value)
    human.db.sex = value
    Status.sendViewStatus(human)
end

function d3.kill(human, account)
    if ObjHuman.onlineAccount[account] then
        ObjHuman.setHp(ObjHuman.onlineAccount[account], 0)
    end
end

function d3.gettask(human, id)
    human.db.taskRecordLen = 0  
    TaskLogic.taskDoAccept(human, id)
    BeginStory.onTaskAccept(human, id)
    human.maxFinishMainTask = TaskHelp.getTaskConfig(id).prevTaskId
    if TaskHelp.getTaskConfig(id).prevTaskId > 0 then
        human.db.taskRecord[human.db.taskRecordLen].taskid = TaskHelp.getTaskConfig(id).prevTaskId
        human.db.taskRecord[human.db.taskRecordLen].status = 2
        human.db.taskRecordLen = human.db.taskRecordLen + 1
    end
    local msgRet = Msg.gc.GC_TASK_ACCEPT
    msgRet.taskID = id
    msgRet.retCode = TaskDefine.TASK_ACCEPT_OK
    Msg.send(msgRet, human.fd)
    TaskLogic.sendTaskList(human, TaskDefine.TASK_LIST_FLUSH_GM)
end

function d3.testday(human)
    d3.zhengdian(human,0)
    ObjHuman.updateDaily(human, true)
    require("role.RoleUIControl").sendLine1FnIcon(human) 
    require("role.RoleUIControl").sendPreknow(human) 
end

function d3.kick(human, account)
    if ObjHuman.onlineAccount[account] then
        ObjHuman.doDisconnect(ObjHuman.onlineAccount[account], CommonDefine.DISCONNECT_NORMAL)
    end
end

function d3.positionmove(human, str)
    local pos = string.find(str, " ", 1, true)
    local x = tonumber(string.sub(str, 1, pos and pos - 1))
    local y = tonumber(string.sub(str, pos + 1))
    Map.through_jump(human.scene_id, human.x, human.y, x, y)
    Status.onJump(human)
    Obj.jump(human, x, y,Msg.gc.GC_SPACE_JUMP)
end

function d3.xy(human)
    send(human, human.x .. " " .. human.y)
end

function d3.addexp(human, val)
    if val > 0 then
        ObjHuman.addExp(human, val)
    elseif val < 0 then
        human.db.exp = human.db.exp + val
        human.attr[RoleDefine.EXP] = human.db.exp
        ObjHuman.sendAttr(human,RoleDefine.EXP)
    end
end
function d3.chongzhi(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "pay"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)
end

function d3.viplist(human)
    VipLogic.sendVipInfo(human)
end

function d3.mail(human, content)
    local msg = Msg.lw.LW_GM
    msg.cmd = "mail"
    msg.arg = ""..content
    Msg.send2world(msg, human._id)
end

function d3.sendmail(human, str)
    local pos = string.find(str, " ", 1, true)
    local title = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local content = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local itemid = tonumber(string.sub(str, 1, pos and pos - 1))

    str = str.sub(str, pos + 1)
    local itemnum = tonumber(str)

    local nGridList = {}
    nGridList[#nGridList + 1] = Grid.create(nil, tonumber(itemid), tonumber(itemnum), 1)
    MailLogicFL.CreateNewMailForUser(human._id, title, content, nGridList)
end

function d3.readmail(human)
    local msg = Msg.cg[Msg.proto_id.CG_READ_MAIL]
    msg.mailID[0] = 1
    msg.mailID = 5
    Util.printTable(msg)
    Msg.send2world(msg, human._id)
end

function d3.askmap(human)
    ConfigHandleL.CG_MAP_CONF_QUERY(human, {mapID = 1001})
end

function d3.savedb(human)
    local msg = Msg.lw.LW_GM
    msg.cmd = "savedb"
    msg.arg = ""
    Msg.send2world(msg, human._id)
end

function d3.lv(human, val)
    if val > 150 then
        val = 150
    end
    local old_lv = human.db.lv
    human.db.lv = val    
    ObjHuman.onLvUpCB(human, old_lv, val)
end

function d3.yinliang(human, val)
    if val > 0 then
        ObjHuman.addYinliang(human, val, "gm")
    elseif val <0 then
        ObjHuman.decYinliang(human, -val, "gm")
    end
end
function d3.yuanbao(human, val)
    if val > 0 then
        ObjHuman.addYuanbao(human, val, "gm")
    elseif val <0 then
        ObjHuman.decYuanbao(human, -val, "gm")
    end
end

function d3.bindyuanbao(human, val)
    if val > 0 then
        ObjHuman.addBindYuanbao(human, val, "gm")
    elseif val <0 then
        human.db.bind_yuanbao = human.db.bind_yuanbao + val
        human.attr[RoleDefine.BIND_YUANBAO] = human.db.bind_yuanbao
        ObjHuman.sendAttr(human, RoleDefine.BIND_YUANBAO)
    end
end
function d3.buf(human,bufID)
    Buffer.add(bufID,human,human)
end
function d3.buffer(human,targetID)
    local str = ""
    if targetID then
        local obj = Obj.objs[targetID] or human
        local bufList = Obj.getBuf(obj)
        str = "obj name: "..Obj.getName(obj).." buffer count: "..#bufList
    else
        local num = 0
        local count = 0
        for objID,uid in pairs(Buffer.list) do
            num = num+1
            local obj = Obj.getObj(objID,uid)
            if obj then
                local bufList = Obj.getBuf(obj)
                count = count+#bufList
            end
        end
        str = "obj num: "..num.." buffer count: "..count.." buffer pool: "..Buffer.pool.length
    end
    send(human,str)
end



function d3.copy(human, mapid)
    CopyHandler.copyEnter(human, mapid+0)
end

function d3.clcopy(human)
    human.db.copyTimesLen = 0   --副本次数清空
end

function d3.skillcd(human, t)
    human.clskillcd = t and t ==1 and 1 or nil
end

function d3.test(human, t)
    local act = require("activity.date.LimitBuy")
    act.open(4, 1)
    --  local msg = Msg.lw.LW_GM
    --  msg.cmd = "test"
    --  msg.arg = ""..t
    --  Msg.send2world(msg, human._id)
end

function d3.qpoint(human, t)
    local msg = Msg.lw.LW_GM
    msg.cmd = "qpoint"
    msg.arg = ""..t
    Msg.send2world(msg, human._id)
end


function d3.map(human, t)
    if t == 400 then
        return
    end
    ObjHuman.enterMap(human, t, ExcelMapConf.scene[t].x, ExcelMapConf.scene[t].y)
end

function d3.mob(human, arg)
    local pos = string.find(arg, " ", 1, true)
    local id = string.sub(arg, 1, pos and pos - 1)
    local num = string.sub(arg, #id+2)

    id = id+0
    num = num+0
    local rect= {}
    for i = 1, num do
        local monster = ObjMonster.create(id)
        rect[1] = human.x-200
        rect[2] = human.y-200
        rect[3] = human.x+200
        rect[4] = human.y+200
        ObjMonster.enterScene(monster, human.scene_id, math.random(rect[1], rect[3]), math.random(rect[2], rect[4]))

    end

end

function d3.collect(human, arg)
    local pos = string.find(arg, " ", 1, true)
    local id = string.sub(arg, 1, pos and pos - 1)
    local num = string.sub(arg, #id+2)

    id = id+0
    num = num+0
    local rect= {}
    for i = 1, num do
        local monster = ObjCollect.create(id)
        rect[1] = human.x-200
        rect[2] = human.y-200
        rect[3] = human.x+200
        rect[4] = human.y+200
        ObjCollect.enterScene(monster, human.scene_id, math.random(rect[1], rect[3]), math.random(rect[2], rect[4]))

    end

end

function d3.zhangong(human, arg)
    if arg > 0 then
        ObjHuman.addZhangong(human,arg, "gm")
    else
        ObjHuman.decZhangong(human, -arg, "gm")
    end
end

function  d3.script(human, arg)
    local mm = Msg.gc.GC_SCRIPT_START
    mm.script_id = arg
    mm.delay = 0
    Msg.send(mm, human.fd)
end

function d3.hp(human, arg)
    ObjHuman.setHp(human, tonumber(arg))
end

function d3.shenbing(human, str)
    local pos = string.find(str, " ", 1, true)
    local id = tonumber(string.sub(str, 1, pos and pos - 1))
    str = str.sub(str, pos + 1)
    pos = str.find(str, " ", 1, true)
    local lv = tonumber(str.sub(str, 1, pos and pos - 1))
    str = str.sub(str, pos + 1)
    pos = str.find(str, " ", 1, true)
    local star = tonumber(str.sub(str, 1, pos and pos - 1))
    local data = ObjHuman.getShenbing(human, id)
    if not data then
        send(human, "神兵未激活")
        return
    end

    if lv > human.db.lv then
        send(human, "等级超过人物等级")
        return
    end

    data.lv = lv
    data.star = star

    ShenbingLogic.onLevelUp(human)
    ShenbingLogic.updateShenbing(human, id)
end

function d3.useskill(human, str)
    local pos = string.find(str, " ", 1, true)
    local id = tonumber(string.sub(str, 1, pos and pos - 1))
    str = str.sub(str, pos + 1)
    pos = str.find(str, " ", 1, true)
    local lv = tonumber(str.sub(str, 1, pos and pos - 1))

    local msg = Msg.gc.GC_DEBUG
    msg.type = 1
    SkillNotify.setSkillData(human, id, msg.skill, lv)
    human.gm_skill_lv = lv
    human.gm_skill_id = id
    Msg.send(msg, human.fd)
end

function d3.fcm_online(human, t)
    human.db.olTime = t
    local str = "today online:"..(os.time()-human.db.lastLoginTime + human.db.olTime)
    send(human, str)
end

function d3.fcm(human, t)
    human.db.fcm = t
end

function d3.rlnow(human)
    RoleRevive.reviveNow(human)
end

function d3.rlhome(human)
    RoleRevive.reviveGoHome(human)
end

function d3.openfamily(human)
    FamilyHandler.CG_FAMILY_OPEN(human)
end

function d3.ctb(human)
    FamilyHandler.CG_FAMILY_CONTRIBUTE(human)
end

local FamilyHandler = require("family.HandlerL")
function d3.createfamily(human)
    d3.lv(human, 35)
    d3.yinliang(human, 1000000000)
    FamilyHandler.CG_FAMILY_CREATE(human, {createType = 0, familyName = "dfkjkek" .. math.random(1, 10000)})
end

function d3.destroyfamily(human)
    local nSend2Wdata = {
        type = -1,
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_FAMILY, nSend2Wdata)
end

function d3.stgq(human)
    FamilyHandler.CG_FAMILY_STRATEGY_QUERY(human, {})
end

function d3.stgup(human)
    FamilyHandler.CG_FAMILY_STRATEGY_UP(human, {id = 1})
end

function d3.familyshop(human)
    FamilyHandler.CG_FAMILY_SHOP_QUERY(human)
end

function d3.flushfamilyshop(human)
    FamilyHandler.CG_FAMILY_SHOP_FLUSH(human)
end

function d3.msshop(human)
    MySteryShop.CG_MYSTERY_SHOP_QUERY(human, {type = 1})
end

function d3.msflush(human)
    MySteryShop.CG_MYSTERY_SHOP_FLUSH(human, {type = 1})
end

function d3.msbuy(human, val)
    MySteryShop.CG_MYSTERY_SHOP_BUY(human, {type = 1, index = val})
end

function d3.msclear(human)
    human.db.mysteryshopFlushCnt = 0
end

function d3.jihuoPet(human)
    PetLogic.jihuoLimitPet(human)
end

function d3.addact(human, str)
    local pos = string.find(str, " ", 1, true)
    local nActNum = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nStartHour = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nStartMin = tonumber(string.sub(str, 1, pos and pos - 1))

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nRuleID = tonumber(string.sub(str, 1, pos and pos - 1))

    str = str.sub(str, pos + 1)
    local nStatus = tonumber(str)

    local nSend2Wdata = {
        type = 1,
        data = {actNum = nActNum, startHour = nStartHour, startMin = nStartMin, ruleID = nRuleID, status = nStatus}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)
end

function d3.endact(human, str)
    local pos = string.find(str, " ", 1, true)
    local nActNum = string.sub(str, 1, pos and pos - 1)
    local nSend2Wdata = {
        type = 3,
        data = {actNum = nActNum}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)
end

function d3.resetact(human)
    local nSend2Wdata = {
        type = 2,
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)
end

function d3.startact(human, arg)
    local nSend2Wdata = {
        type = 5,
        data = {actType = arg}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)

    local msg = Msg.lw.LW_GM
    msg.cmd = "middlestartact"
    msg.arg = ""..arg
    Msg.send2world(msg, human._id)
end

function d3.closeact(human, arg)
    local nSend2Wdata = {
        type = 6,
        data = {actType = arg}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)

    local msg = Msg.lw.LW_GM
    msg.cmd = "middlecloseact"
    msg.arg = ""..arg
    Msg.send2world(msg, human._id)
end

function d3.actlist(human)
    local nSend2Wdata = {
        type = 4,
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Wdata)
end

function d3.finishnew(human)
    human.db.isMount = 0
    human.db.chooseHorseId = 1
    Buffer.clearAllBuff(human)
    d3.gettask(human,15)
    ShenbingLogic.chooseShenbing(human, 1)
    d3.lv(human, 5)
    d3.map(human,9999)    
    HorseLogic.mountHorse(human)
end

function d3.clearmonster(human)
    for _, v in pairs(Obj.scenes[human.scene_id]) do
        if v.monster_id then
            ObjMonster.destroy(v)
        end
    end
end
function d3.clearbuf(human, id)
    Buffer.removeID(id, human)
end
function d3.pata(human,  guan)
    human.db.pata_cur = guan
    if human.db.pata_cur > human.db.pata_max then
        human.db.pata_max = guan
    end
end
function d3.getpos(human, id)
    for _, v in pairs(Obj.scenes[human.scene_id]) do
        if v.monster_id  == id then
            print("getpos: id:", v.id, v.x, v.y)
        end
    end
end

function d3.notice(human, str)
    local nSend2Wdata = {
        type = 1,
        data = {notice = str}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Wdata)
end

function d3.version(human, str)
    local nSend2Wdata = {
        type = 2,
        data = {version = str}
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Wdata)
end

function d3.serveropentime(human, str)
    local pos = string.find(str, " ", 1, true)
    local nYear = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nMon = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nDay = tonumber(string.sub(str, 1, pos and pos - 1))

    local nDate = os.time({year = nYear, month = nMon, day = nDay, hour = 0, min = 0, sec = 0})

    local nSend2Wdata = {
        type = 3,
        data = nDate 
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Wdata)
end

function d3.servercomplextime(human, str)
    local pos = string.find(str, " ", 1, true)
    local nYear = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nMon = string.sub(str, 1, pos and pos - 1)

    str = str.sub(str, pos + 1)
    pos = string.find(str, " ", 1, true)
    local nDay = tonumber(string.sub(str, 1, pos and pos - 1))

    local nDate = os.time({year = nYear, month = nMon, day = nDay, hour = 0, min = 0, sec = 0})

    local nSend2Wdata = {
        type = 4,
        data = nDate 
    }
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SERVERCOMMON, nSend2Wdata)
end

function d3.monster_find(human)
    local list = {}
    local x, y = human.x, human.y
    for i = -1, 1 do
        for j = -1, 1 do
            if not (i == 0 and j == 0 or i == 0 and j == 1) then
                list[#list + 1] = {x + j * 100, y + i * 100}
            end
        end
    end
    list[#list + 1] = {x, y + 100}
    for k, v in ipairs(list) do
        local monster = ObjMonster.create(1001801)
        ObjMonster.enterScene(monster, human.scene_id, v[1], v[2])
    end
end

function d3.god(human, val)
    d3.yinliang(human, 99999999)
    d3.bindyuanbao(human, 99999999)
    d3.lv(human, 60)
    d3.map(human, 2002)
    d3.zhangong(human, 100000)
    d3.gettask(human, 1300)
    if human.db.shenbingLen == 0 then
        local id = 1
        if val == 2 or val == 3 then
            id = val
        end
        ShenbingLogic.chooseShenbing(human, id)
    end
    RoleUIControl.sendMenuRightControl(human)
end

function d3.talk()
    require("monster.MonsterAi").needTalk = true
    require("pet.PetAi").need_talk = true
end

function d3.jzwar()
    print("GM ", JZWar.HumanWarDataList)
    JZWar.onJZWarRoomEnd()
end

function d3.clshenbin(human)
    human.db.shenbingLen = 0
end

function d3.addweiwang(human, arg)
    ObjHuman.addPrestige(human, arg, "gm")
end

function d3.addrongyu(human, arg)
    ObjHuman.addRongyu(human, arg, "gm")
end

function d3.showmem(human)
    local msg = Msg.lw.LW_GM
    msg.cmd = "showmem"
    msg.arg = ""
    Msg.send2world(msg, human._id)
end

function d3.testpk(human, name)
    local targetHuman = ObjHuman.onlineHuman[name]
    if not targetHuman then
        send(human, "目标:"..name.."没有在同一地图")
        return
    end
    require("offlinepk.OfflinePkLogicL").startPk(human, targetHuman._id)
end

function d3.wudi(human)
    human.wudi = not human.wudi
    send(human, human.wudi and "已经无敌了" or "已经不是无敌了")
end

function d3.openday(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "openday"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)
end

function d3.wildbossflush(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "wildbossflush"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)
end

function d3.zhengdian(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "zhengdian"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)
end

function d3.testdrop(human, len)
    local mx = 2662
    local my = 2612

    local xy = Map.drop_item(human.scene_id, mx, my, len+0, mx, my)
    if xy then
        for i = 1, len do

            if not Map.can_walk(human.scene_id, xy[i * 2 - 1], xy[i * 2]) then
                print("i:"..i, xy[i * 2 - 1], xy[i * 2], "no")
            else
                print("i:"..i, xy[i * 2 - 1], xy[i * 2])
            end
        end
    end

    local a =  Map.can_walk(human.scene_id, mx, my) 
    print("Can walk:", mx,my, a)
end


function d3.taskmarket(human, id)
    local msg = Msg.lw.LW_GM
    msg.cmd = "taskmarket"
    msg.arg = ""..id
    Msg.send2world(msg, human._id)
end


function d3.testweiyan(human)
    local pet = Obj.getObj(human.fight_pet_id, human.fight_pet_uid)
    if pet then
        for _, obj in pairs(Obj.scenes[human.scene_id]) do
            if obj.obj_type == Obj.TYPE_MONSTER then
                Obj.jump(obj, pet.x, pet.y, Msg.gc.GC_JUMP)
            end 
        end
    end
end

function d3.middleopen(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "middleopen"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)	
end

function d3.fwar(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "middlefamilywar"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)  
end


function d3.middleenter(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "middleenter"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)  
end
function d3.middleleave(human, val)
    local msg = Msg.lw.LW_GM
    msg.cmd = "middleleave"
    msg.arg = ""..val
    Msg.send2world(msg, human._id)  
end

function d3.tgp(human)
    human.db.isMiniClient = 14

    human.db.tgpDaily = 0
    human.db.tgpNew = 0
    human.db.tgpLv = {[0] = 0,0,0,0,0,0,0,0,0,0}

    require("role.RoleUIControl").sendLine1FnIcon(human)
end

function d3.dailygift(human)
    human.db.dailyGiftStatus = 1
    require("activity.operate.DailyGift").sendDailyGift(human)
end

function d3.armouropen(human)
    require("armour.HandlerL").CG_ARMOUR_OPEN(human)
end

function d3.armouractive(human)
    require("armour.HandlerL").CG_ARMOUR_ACTIVE(human, {id = 1}, true)
end

function d3.armourupdate(human, val)
    local lv = tonumber(val or 1)
    require("armour.HandlerL").CG_ARMOUR_UPDATE(human, {id = 1}, lv)
end

function d3.armourhold(human, val)
    local type = tonumber(val)
    if type ~= 0 and type ~= 1 then
        return
    end
    require("armour.HandlerL").CG_ARMOUR_HOLD(human, {id = 1, type = type})
end

function d3.armourstarup(human, val)
    local star = tonumber(val or 1)
    require("armour.HandlerL").CG_ARMOUR_STARUP(human, {id = 1}, star)
end

function d3.search(human)
    local msg = Msg.lw.LW_GM
    msg.cmd = "search"
    msg.arg = ""
    Msg.send2world(msg, human._id)  
end

function d3.blueexpire(human, val)
   local mm = Msg.gc.GC_QQ_BLUE_EXPIRE
   mm.blue = 0
   mm.blueYear = 0 
   mm.blueSuper = 0
   if val == 1 then
      mm.blueSuper = 1
      mm.expire = 1
   else
      mm.blue = 1
      mm.expire = 1
   end
   mm.reward[0] = 0
   Msg.send(mm, human.fd)   
end

function d3.testmonster(human)
    local m1 = ObjMonster.create(7)
    local m2 = ObjMonster.create(7)
    
    ObjMonster.enterScene(m1, human.scene_id, human.x-100, human.y)
    ObjMonster.enterScene(m2, human.scene_id, human.x+100, human.y)
    
end

function d3.addtitle(human, val)
  require("title.TitleLogic").active(human, val)
  require("title.TitleLogic").sendTitleList(human)
  require("title.TitleLogic").sendHumanTitle(human)
end
a = a or {}
function d3.test21(human, val)
  for i = 1, 100000 do
      a[#a+1] = {index=i, str="sssss:"..i}
  end
  collectgarbage("collect")
  print("logic:".._G.logic_id.." :"..collectgarbage("count"))
end

function d3.official(human)
  human.db.officialLv = 1
  require("official.OfficialLogic").checkAllCondition(human)
  require("official.OfficialLogic").sendOfficialInfo(human)
end
